package com.pengpeng.android.activity;


    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.Window;

    import com.baidu.mapapi.BMapManager;
    import com.baidu.mapapi.GeoPoint;
    import com.baidu.mapapi.MKPlanNode;
    import com.baidu.mapapi.MKSearch;
    import com.baidu.mapapi.MapActivity;
    import com.baidu.mapapi.MapController;
    import com.baidu.mapapi.MapView;
import com.pengpeng.android.activity.R;
    import com.pengpeng.android.entity.MapEntity;
    import com.pengpeng.android.framework.SystemShareObjects;
import com.pengpeng.android.logic.main.BaiduMap;
import com.pengpeng.android.logic.main.MySearchListener;
import com.pengpeng.android.logic.main.OptM_MainMenuManager;
import com.pengpeng.android.logic.main.TitB_MainTitleBar;

    public class BaiduMapActivity extends MapActivity {
    private BMapManager mBMapMan = null;
    private MKSearch    mMKSearch=null;
    private MapView     mMapView=null;
    private OptM_MainMenuManager optmenumanager=null;
 
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    super.onCreate(savedInstanceState);
    
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    
    setContentView(R.layout.main_baidumap);
    
    TitB_MainTitleBar inittitlebar=new TitB_MainTitleBar();
    inittitlebar.load((Activity)this);
    
    mBMapMan = new BMapManager(getApplication());
    mBMapMan.init("08DD8580F0C0EE3E39F55A56F90ACCA12AFB11A0",null);
    super.initMapActivity(mBMapMan);
     
    mMapView = (MapView) findViewById(R.id.bmapsView);
    
    BaiduMap bmap=new BaiduMap(mBMapMan,mMapView);
    SystemShareObjects SObjContainer=SystemShareObjects.getInstance();
    SObjContainer.setMap(bmap);
    
    MapEntity mapentity=SObjContainer.getMapEntity();
    BaiduMap bmap2=(BaiduMap)SObjContainer.getMap();
   
    mMapView.setBuiltInZoomControls(true); //设置启用内置的缩放控件
     
    initMap(mapentity,bmap2);

     bmap2.setZoom(12);
     bmap2.setCenter(39.915, 116.404);
    
  /*  mMKSearch = new MKSearch();
    mMKSearch.init(mBMapMan, new MySearchListener(this,mMapView));
    
    MKPlanNode start = new MKPlanNode();
    start.pt = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6));
    MKPlanNode end = new MKPlanNode();
    end.pt = new GeoPoint(40057031, 116307852);
    // 设置驾车路线搜索策略，时间优先、费用最少或距离最短
    mMKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
    mMKSearch.drivingSearch(null, start, null, end);
    */
    
    }
     
    private void initMap(MapEntity mapentity,BaiduMap bmap)
    {
    	 String maptype="";
    	 String initlocation;
    	 String initloc_array[];
    	 Integer zoomlevel=12;
    	 String viewmode="";  //'1':normalview 普通模式 ,2:streetview 实景模式,3:satelliteview 卫星模式
         String iftraffic=""; //是否显示路况
    	 String routemode=""; //0：不显示路线，1：步行路线，2：驾车路线，3：公交路线
    	
    	maptype=mapentity.getMaptype();
    	initlocation=mapentity.getInitlocation();
    	zoomlevel=Integer.parseInt(mapentity.getZoomlevel());
    	viewmode=mapentity.getViewmode();
    	iftraffic=mapentity.getIftraffic();
    	routemode=mapentity.getRoutemode();
    	
    	initloc_array=initlocation.split(",");
    	
    	if(initloc_array.length==2)
    	{
    		bmap.setCenter(Double.parseDouble(initloc_array[0]), Double.parseDouble(initloc_array[1]));
    	}
    	else
    	{
    		//记录异常
    		bmap.setCenter(39.915, 116.404);
    	}
    	
    	bmap.setZoom(zoomlevel);

    	
    
    }
    
    @Override
    protected boolean isRouteDisplayed() {
    return false;
    }
    
    @Override
    protected void onDestroy() {
     if (mBMapMan != null) {
         mBMapMan.destroy();
         mBMapMan = null;
     }
     super.onDestroy();
    }
    @Override
    protected void onPause() {
    if (mBMapMan != null) {
    mBMapMan.stop();
    }
    super.onPause();
    }
    @Override
    protected void onResume() {
    if (mBMapMan != null) {
    mBMapMan.start();
    }
    super.onResume();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
     	MenuInflater minflater=getMenuInflater();
    	minflater.inflate(R.menu.main_optionsmenu, menu);
    	return(super.onCreateOptionsMenu(menu));
    }//onCreateOptionsMenu
    
    @Override
    public boolean onOptionsItemSelected(MenuItem menuitem)
    {   
    	if(optmenumanager==null)
    	{
    		optmenumanager=new OptM_MainMenuManager((Activity)this);
    	}
    	
    	switch(menuitem.getItemId())
    	{
    	   case R.id.MI_SEARCH:
    	   {
    		   optmenumanager.dosearch();
    		   break;
    	   }
    	   case R.id.MI_ROUTE:
    	   {
    		   optmenumanager.doroute(); 
    		   break;
    	   }
    	   case R.id.MI_LEVEL:
    	   {
    		   optmenumanager.dolevel();
    		   break;
    	   }
    	   case R.id.MI_FRIENDSMANAGE:
    	   {
    		   optmenumanager.dofriendsmanage();
    		   break;
    	   }
    	   case R.id.MI_CONFIG:
    	   {
    		   optmenumanager.doconfig();
    		   break;
    	   }
    	   case R.id.MI_EXIT:
    	   {
    		   optmenumanager.doexit();
    		   break;
    	   }
    	}
    	return(super.onOptionsItemSelected(menuitem));
    }//onOptionsItemSelected

 }
